-- DROP FUNCTION question_type_audit();

CREATE OR REPLACE FUNCTION question_type_audit()
  RETURNS trigger AS
$BODY$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO question_type_audit SELECT nextval('question_type_audit_seq'),'D', now(), OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO question_type_audit SELECT nextval('question_type_audit_seq'),'U', now(), NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO question_type_audit SELECT nextval('question_type_audit_seq'),'I', now(), NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; -- result is ignored since this is an AFTER trigger
    END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION question_type_audit() OWNER TO postgres;

CREATE TRIGGER question_type_audit
    AFTER INSERT OR DELETE OR UPDATE ON question_type
    FOR EACH ROW
    EXECUTE PROCEDURE question_type_audit();

-- DROP FUNCTION Category_Type_Audit();

CREATE OR REPLACE FUNCTION Category_Type_Audit()
  RETURNS trigger AS
$BODY$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO Category_Type_Audit SELECT nextval('Category_Type_Audit_seq'),'D', now(), OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO Category_Type_Audit SELECT nextval('Category_Type_Audit_seq'),'U', now(), NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO Category_Type_Audit SELECT nextval('Category_Type_Audit_seq'),'I', now(), NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; -- result is ignored since this is an AFTER trigger
    END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION Category_Type_Audit() OWNER TO postgres;

CREATE TRIGGER Category_Type_Audit
    AFTER INSERT OR DELETE OR UPDATE ON category_type
    FOR EACH ROW
    EXECUTE PROCEDURE Category_Type_Audit();

-- DROP FUNCTION Level_Type_Audit();

CREATE OR REPLACE FUNCTION Level_Type_Audit()
  RETURNS trigger AS
$BODY$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO Level_Type_Audit SELECT nextval('Level_Type_Audit_seq'),'D', now(), OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO Level_Type_Audit SELECT nextval('Level_Type_Audit_seq'),'U', now(), NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO Level_Type_Audit SELECT nextval('Level_Type_Audit_seq'),'I', now(), NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; -- result is ignored since this is an AFTER trigger
    END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION Level_Type_Audit() OWNER TO postgres;

CREATE TRIGGER Level_Type_Audit
    AFTER INSERT OR DELETE OR UPDATE ON level_type
    FOR EACH ROW
    EXECUTE PROCEDURE Level_Type_Audit();

-- DROP FUNCTION Answer_Type_Audit();

CREATE OR REPLACE FUNCTION Answer_Type_Audit()
  RETURNS trigger AS
$BODY$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO Answer_Type_Audit SELECT nextval('Answer_Type_Audit_seq'),'D', now(), OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO Answer_Type_Audit SELECT nextval('Answer_Type_Audit_seq'),'U', now(), NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO Answer_Type_Audit SELECT nextval('Answer_Type_Audit_seq'),'I', now(), NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; -- result is ignored since this is an AFTER trigger
    END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION Answer_Type_Audit() OWNER TO postgres;

CREATE TRIGGER Answer_Type_Audit
    AFTER INSERT OR DELETE OR UPDATE ON answer_type
    FOR EACH ROW
    EXECUTE PROCEDURE Answer_Type_Audit();

-- DROP FUNCTION Exam_Type_Audit();

CREATE OR REPLACE FUNCTION Exam_Type_Audit()
  RETURNS trigger AS
$BODY$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO Exam_Type_Audit SELECT nextval('Exam_Type_Audit_seq'),'D', now(), OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO Exam_Type_Audit SELECT nextval('Exam_Type_Audit_seq'),'U', now(), NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO Exam_Type_Audit SELECT nextval('Exam_Type_Audit_seq'),'I', now(), NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; -- result is ignored since this is an AFTER trigger
    END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION Exam_Type_Audit() OWNER TO postgres;

CREATE TRIGGER Exam_Type_Audit
    AFTER INSERT OR DELETE OR UPDATE ON exam_type
    FOR EACH ROW
    EXECUTE PROCEDURE Exam_Type_Audit();